# coding: UTF-8
# Filename : member_action.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl
from api.points_record import points_record_manage
import time
from MemberCard import *

#计算会员卡号
def get_member_num(cus_id):
    sql = 'select length, start_num, end_num, filter_nums from card_config where cus_id = %s'
    config_obj = mysql.get(sql, [cus_id])
    number = ''
    if config_obj == None:
        number = utils.get_random_str(6)
        return number
    length = config_obj[0]
    start_num = config_obj[1]
    end_num = config_obj[2]
    filter_nums = config_obj[3]
    if filter_nums == None or filter_nums.strip() == '': filter_nums = '0'
    get_middle_length = length - len(str(start_num)) - len(str(end_num))
    #获取最后一位会员卡号 如果当前为第一位会员则初始化
    lmn_sql = 'select tb1.number from member tb1 where tb1.id = (select max(tb2.id) from member tb2)'
    lmn_obj = mysql.get(lmn_sql, None)
    if lmn_obj == None:
        number = str(start_num) + str(utils.format_num(get_middle_length, 1)) + str(end_num)
    else:
        last_member_num = lmn_obj[0]
        middle_member = int((str(last_member_num)).replace(str(start_num), '').replace(str(end_num), ''))
        now_number = middle_member + 1
        filter_nums_arr = str(filter_nums).split(',')
        for obj in filter_nums_arr:
            now_number = filter_num(now_number, obj)
        number = str(start_num) + str(utils.format_num(get_middle_length, now_number)) + str(end_num)
    return number

#过滤数字中的某个字符
def filter_num(num, f_num):
    num_str = str(num)
    num_str = num_str.replace(str(f_num), str(int(f_num)+1))
    return int(num_str)

#获取会员审核标识位
def get_join_way(cus_id):
    get_join_way_sql = 'select join_way from card_config where cus_id = %s'
    join_way_obj = mysql.get(get_join_way_sql, [cus_id])
    if join_way_obj == None : return 2
    return join_way_obj[0]

#插入会员记录
def insert_member(name, gender, email, birthday, idcard, user_id, cus_id):
    join_way = get_join_way(cus_id)
    state = join_way #当为1、2就直接赋值
    #当为0时候 表示注册直接变成会员
    if join_way == 0: state = 1
    
    insert_sql = '''insert  into `member` 
    (`user_id`, `cus_id`, `number`, `level`, `name`, `gender`,
     `birthday`, `id_number`, `email`, `total_score`, `curr_score`, 
     `created_at`, `updated_at`, `state`) 
     values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
     
    update_sql = '''
    update member set `number`=%s, `level`=%s, `name`=%s, `gender`=%s,
     `birthday`=%s, `id_number`=%s, `email`=%s, `total_score`=%s, `curr_score`=%s, 
     `created_at`=%s, `updated_at`=%s, `state`=%s
     where user_id = %s and cus_id = %s
    '''
    number = get_member_num(cus_id)
    level = 1
    id_number = idcard
    #获取申请会员积分维度
    total_score = points_record_manage.get_points_dimensionality(user_id, cus_id, 'member_points')
    if total_score == None: total_score = 0
    curr_score = total_score
    created_at = int(time.time())
    updated_at = created_at
    param_insert = [user_id, cus_id, number, level, name, gender, birthday, id_number, email, total_score, curr_score, created_at, updated_at, state]
    param_update = [number, level, name, gender, birthday, id_number, email, total_score, curr_score, created_at, updated_at, state, user_id, cus_id]
    
    is_exist = 'select count(id) from member where user_id = %s and cus_id = %s'
    obj = mysql.get(is_exist, [user_id, cus_id])
    count = obj[0]
    if count == 0:
        is_ok = mysql.insert(insert_sql, param_insert)
    else:
        is_ok = mysql.update(update_sql, param_update)
    
    return is_ok

#得到会员信息
def get_member_info(user_id, cus_id):
    mc = MemberCard()
    #查出会员等级 积分 卡号
    member_sql = '''select tb1.level, tb1.curr_score, tb1.number, tb1.total_score,
    (select tb2.name from member_level tb2 where tb2.cus_id = %s and tb2.level = tb1.level)
    from member tb1
    where user_id = %s and cus_id = %s'''
    member_info = mysql.get(member_sql, [cus_id, user_id, cus_id])
    if member_info == None or len(member_info) == 0: return None
    level = member_info[0]
    curr_score = member_info[1]
    number = member_info[2]
    total_score = member_info[3]
    level_name = member_info[4]
    
    mc.number = number
    mc.level = level
    mc.curr_score = curr_score
    mc.total_score = total_score
    mc.level_name = level_name
    
    #查出会员卡名称 使用须知 有效期 背景 字体颜色 字体大小
    card_info_sql = '''select tb1.name, tb1.use_notice, tb1.useful_life,
                        tb2.background, tb2.number_color, tb2.font_size
                        from card_config tb1, card_style tb2
                        where tb1.style_id = tb2.id
                        and cus_id = %s and tb2.level = %s'''
    card_obj = mysql.get(card_info_sql, [cus_id, level])
    if card_obj == None or len(card_obj) == 0: return mc
    card_name = card_obj[0]
    use_notice = card_obj[1]
    useful_life = card_obj[2]
    background = card_obj[3]
    number_color = card_obj[4]
    font_size = card_obj[5]
    
    mc.name = card_name
    mc.use_notice = use_notice
    mc.useful_life = useful_life
    mc.background = background
    mc.number_color = number_color
    mc.font_size = font_size
    
    #获取二维码图片地址
    qrcode_sql = '''select path  from qrcode where cus_id = %s'''
    qrcode_obj= mysql.get(qrcode_sql, [cus_id])
    if qrcode_obj == None or len(qrcode_obj) == 0: return mc
    path = qrcode_obj[0]
    mc.path = path
    
    return mc

#获取会员声明  
def get_member_statement(user_id, cus_id):
    sql = 'select statement from card_config where cus_id = %s'
    statement_obj = mysql.get(sql, [cus_id])
    statement = ''
    if statement_obj != None:
        statement = statement_obj[0]
    return statement

#获取会员申请状态
def get_state(user_id, cus_id):
    ms = MemberState()
    sql = '''select tb1.state, tb2.username from member tb1, user tb2 where tb1.user_id = tb2.id and tb1.user_id = %s and tb1.cus_id = %s'''
    member_obj = mysql.get(sql, [user_id, cus_id])
    if member_obj == None: return ms
    ms.success = member_obj[0]
    phone_num = str(member_obj[1])
    ms.phone_num = phone_num[0:3] + '****' + phone_num[7:11]
    return ms

#意见反馈
def feedback(user_id, cus_id, phone_num, content):
    if phone_num == None or phone_num.strip() == '':
        user_obj_sql = 'select username from user where id = %s'
        user_obj = mysql.get(user_obj_sql, [user_id])
        if user_obj == None: phone_num = ''
        else:phone_num = user_obj[0]
    sql = '''insert  into `feedback` (`cus_id`, `user_id`, `phone_num`, `content`, `created_at`, `updated_at`, `state`) values (%s, %s, %s, %s, %s, %s, %s)'''
    rst = mysql.insert(sql, [cus_id, user_id, phone_num, content, utils.getTimestamp(), utils.getTimestamp(), 1])
    return rst

#获取注册列表
def get_apply_columns(user_id, cus_id):
    rst = []
    sql = '''select application_form from card_config where cus_id = %s'''
    card_config_obj = mysql.get(sql, [cus_id])
    if card_config_obj == None: return rst
    columns_str = card_config_obj[0]
    columns = columns_str.split(',')
    for column in columns:
        rst.append(column)
    return rst

#检查是否有此user_id 和cus_id
def check_is_exist(user_id, cus_id):
    sql_user_id = 'select count(id) from user where id = %s'
    sql_cus_id = 'select count(id) from chain_config where cus_id = %s'
    user_obj = mysql.get(sql_user_id, [user_id])
    cus_obj = mysql.get(sql_cus_id, [cus_id])
    flag = True
    if user_obj[0] == 0 or cus_obj[0] == 0: flag = False
    return flag
    
    